#include <stdlib.h>
#include <string.h>
#include <poll.h>

#include <vwm.h>

#include "signals.h"

void
vwmterm_set_SIGIO(void)
{
    struct sigaction  action;
    sigset_t          old_mask;

    // action = (struct sigaction*)calloc(1,sizeof(struct sigaction));
    memset(&action,0,sizeof(action));

    // retrieve current signal mask
    sigprocmask(0,NULL,&old_mask);

    action.sa_flags = SA_SIGINFO;
    action.sa_sigaction = vwmterm_SIGIO;
    action.sa_mask = old_mask;

    // on Linux SIGIO and SIGPOLL should be synonyms.  this is for all others
#ifdef SIGIO
    sigaction(SIGIO,(const struct sigaction*)&action,NULL);
#else
    sigaction(SIGPOLL,(const struct sigaction*)&action,NULL);
#endif

    // free(action);

    return;
}

void
vwmterm_SIGIO(int signum,siginfo_t *siginfo,void *unused)
{
    int     fd;
    short   revents;

#ifdef SIGIO
    if(signum != SIGIO) return;
#else
    if(signum != SIGPOLL) return;
#endif

    if(siginfo == NULL) return;

    fd = siginfo->si_fd;
    revents = siginfo->si_band;

    // something there to read
    if(revents & POLLIN)
    {
	    vwm_ui_accel(1);
    }

   return;
}

